<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" append-to-body size="780" destroy-on-close :close-on-click-modal="false"  @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px">
			<el-form-item label="字典类型" prop="dictType">
				<el-input v-model="form.dictType" :disabled="true" />
			</el-form-item>
			<el-form-item label="字典键名" prop="dictLabel">
				<el-input v-model="form.dictLabel" placeholder="字典键名，例如：大学" />
			</el-form-item>
			<el-form-item label="字典键值" prop="dictValue">
				<el-input v-model="form.dictValue" placeholder="字典键值，例如：college" />
			</el-form-item>
			<el-form-item label="样式属性" prop="cssClass">
				<el-input v-model="form.cssClass" placeholder="" />
			</el-form-item>
			<el-form-item label="回显样式" prop="listClass">
				<el-select v-model="form.listClass">
					<el-option v-for="item in listClassOptions" :key="item.value" :label="item.label + '(' + item.value + ')'" :value="item.value">
						<span style="float: left;" :class="item.value">{{ item.label }}</span>
						<span style="float: right;color: var(--el-text-color-secondary);font-size: 13px;">{{ item.value }}</span>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="显示排序" prop="dictSort">
				<el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<jd-radio v-model="form.status" dict="sys_normal_disable"></jd-radio>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
			<el-input v-model="form.remark" type="textarea" :rows="5" placeholder=""></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
			<el-button @click="visible=false" >取 消</el-button>
		</template>
	</el-drawer>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增字典数据',
					edit: '编辑字典数据'
				},
				visible: false,
				isSaveing: false,
				form: {
					dictType: "",
					dictLabel: "",
					dictValue: "",
					cssClass: "",
					dictSort: 1,
					status: "0",
					remark: ""
				},
				rules: {
					dictType: [
						{required: true, message: ''}
					],
					dictLabel: [
						{required: true, message: ''}
					],
					dictValue: [
						{required: true, message: ''}
					]
				},
				dic: [],
				dicProps: {
					value: "id",
					label: "name",
					emitPath: false,
					checkStrictly: true
				},
				listClassOptions: [
					{ label: "默认",value: "default" },
					{ label: "主要",value: "primary" },
					{ label: "成功",value: "success" },
					{ label: "信息",value: "info" },
					{ label: "警告",value: "warning" },
					{ label: "危险",value: "danger" }
				],
			}
		},
		mounted() {
			if(this.params){
				this.form.dic = this.params.code
			}
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = {}
						if (this.mode == 'add'){
							res = await this.$API.system.dict.data.addData.post(this.form);
						} else if (this.mode == 'edit') {
							res = await this.$API.system.dict.data.editData.put(this.form);
						}
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				Object.assign(this.form, data);
			}
		}
	}
</script>

<style>
</style>